package com.shareach.http.shop.rpc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shareach.dao.redis.impl.MatchRDao;
import com.shareach.dao.redis.impl.SnatchRDao;
import com.shareach.domain.BaseCityDomain;
import com.shareach.domain.BizCardDomain;
import com.shareach.domain.DiscountType;
import com.shareach.http.shop.parameter.BizCardParameter;
import com.shareach.service.BaseCityService;
import com.shareach.service.BizCardService;
import com.shareach.util.DateUtils;
import com.shareach.util.Globals;
import com.shareach.util.HelperUtil;
import com.shareach.web.basic.Answer;
import com.shareach.web.basic.Ask;
import com.shareach.web.basic.IAskParameter;
import com.shareach.web.basic.IAskRPC;

@Service("cardUploadRpc")
public class CardUploadRpc implements IAskRPC {
	@Autowired
	private BizCardService bizCardService;
	@Autowired
	private BaseCityService baseCityService;
	@Autowired
	private SnatchRDao snatchRDao;
	@Autowired
	private MatchRDao matchRDao;
	final Logger logger = Logger.getLogger("quartz");
	@Override
	public Answer<?> run(Ask ask, String ip) throws Exception {
		BizCardParameter p = (BizCardParameter) getParameterMap(ask);
		Answer<Long> result = this.getAnswer();

		BizCardDomain domain = new BizCardDomain();

		if (StringUtils.isEmpty(p.title)) {
			result.setCode(-1);
			result.setMsg("该条数据没有title,不处理");
			logger.info("该条数据没有title,不处理:::"+ask.getSn());
			return result;
		}
		
		// 用来判断是否采集
		snatchRDao.setSnatchedSiteId("bankCard", p.site, p.siteFromId);

		domain.setSite(p.site);
		domain.setSiteId(p.siteFromId);
		domain.setTitle(p.title);
		domain.setDescry(p.descry);
		if (p.end != null) {

			domain.setEnd(DateUtils.parse(p.end));
		}

		domain.setImg(p.img);
		domain.setCity(Long.parseLong(p.city));
		domain.setBrandName(p.brandName);
		domain.setName(p.name);

		domain.setOrgName(p.orgName);
		domain.setUrl(p.url);

		BaseCityDomain city = baseCityService.getById(Long.parseLong(p.city));
		if (p.tels != null && !p.tels.isEmpty()) {
			List<String> tels = new ArrayList<String>();
			for (String t : p.tels) {
				if (StringUtils.isBlank(t))
					continue;
				List<String> telCodes = HelperUtil.getTels(city.getTel(), t);
				for (String tel : telCodes) {
					if (StringUtils.isBlank(tel))
						continue;
					tels.add(t);
				}
			}
			if (tels.size() > 0) {
				domain.setTels(tels.toArray(new String[tels.size()]));
			}
		}
		domain.setDiscount(p.discount);
		domain.setAddr(p.addr);
		domain.setStatus(p.status);

		Map<String, Object> retMap = bizCardService.saveOrUpdate(domain);
		String retCode = (String) retMap.get(Globals.GLOBAL_SERVICE_RET_CODE);
		// 匹配队列使用
		if (retCode.equals(Globals.GLOBAL_SERVICE_CODE_NOTEXIST)) {
			matchRDao.addDiscountShop(DiscountType.Card, (Long) retMap.get(Globals.GLOBAL_SERVICE_RET_ID));
		}

		return result;
	}

	@Override
	public Answer<Long> getAnswer() {
		return new Answer<Long>();
	}

	@Override
	public IAskParameter getParameterMap(Ask ask) {
		BizCardParameter parameter = new BizCardParameter();
		HashMap<String, Object> map = ask.getRequest();
		parameter.parseFromMap(map);
		return parameter;
	}
}
